<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyDate">
      <el-input v-model="dataForm.modifyDate" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="账号名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="账号名称"></el-input>
    </el-form-item>
    <el-form-item label="账号密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="账号密码"></el-input>
    </el-form-item>
    <el-form-item label="证书" prop="certificate">
      <el-input v-model="dataForm.certificate" placeholder="证书"></el-input>
    </el-form-item>
    <el-form-item label="私钥" prop="privateKey">
      <el-input v-model="dataForm.privateKey" placeholder="私钥"></el-input>
    </el-form-item>
    <el-form-item label="描述文件" prop="descriptionFile">
      <el-input v-model="dataForm.descriptionFile" placeholder="描述文件"></el-input>
    </el-form-item>
    <el-form-item label="开发者账号APP ID " prop="acountApp">
      <el-input v-model="dataForm.acountApp" placeholder="开发者账号APP ID "></el-input>
    </el-form-item>
    <el-form-item label="状态是否可用 0：默认  1：不可用" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态是否可用 0：默认  1：不可用"></el-input>
    </el-form-item>
    <el-form-item label="保留字段token" prop="token">
      <el-input v-model="dataForm.token" placeholder="保留字段token"></el-input>
    </el-form-item>
    <el-form-item label="失效时间" prop="expiredDate">
      <el-input v-model="dataForm.expiredDate" placeholder="失效时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createDate: '',
          modifyDate: '',
          userName: '',
          password: '',
          certificate: '',
          privateKey: '',
          descriptionFile: '',
          acountApp: '',
          status: '',
          token: '',
          expiredDate: ''
        },
        dataRule: {
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyDate: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '账号名称不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '账号密码不能为空', trigger: 'blur' }
          ],
          certificate: [
            { required: true, message: '证书不能为空', trigger: 'blur' }
          ],
          privateKey: [
            { required: true, message: '私钥不能为空', trigger: 'blur' }
          ],
          descriptionFile: [
            { required: true, message: '描述文件不能为空', trigger: 'blur' }
          ],
          acountApp: [
            { required: true, message: '开发者账号APP ID 不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态是否可用 0：默认  1：不可用不能为空', trigger: 'blur' }
          ],
          token: [
            { required: true, message: '保留字段token不能为空', trigger: 'blur' }
          ],
          expiredDate: [
            { required: true, message: '失效时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/st/staccount/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createDate = data.stAccount.createDate
                this.dataForm.modifyDate = data.stAccount.modifyDate
                this.dataForm.userName = data.stAccount.userName
                this.dataForm.password = data.stAccount.password
                this.dataForm.certificate = data.stAccount.certificate
                this.dataForm.privateKey = data.stAccount.privateKey
                this.dataForm.descriptionFile = data.stAccount.descriptionFile
                this.dataForm.acountApp = data.stAccount.acountApp
                this.dataForm.status = data.stAccount.status
                this.dataForm.token = data.stAccount.token
                this.dataForm.expiredDate = data.stAccount.expiredDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/st/staccount/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createDate': this.dataForm.createDate,
                'modifyDate': this.dataForm.modifyDate,
                'userName': this.dataForm.userName,
                'password': this.dataForm.password,
                'certificate': this.dataForm.certificate,
                'privateKey': this.dataForm.privateKey,
                'descriptionFile': this.dataForm.descriptionFile,
                'acountApp': this.dataForm.acountApp,
                'status': this.dataForm.status,
                'token': this.dataForm.token,
                'expiredDate': this.dataForm.expiredDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
